import 'package:flutter/widgets.dart';

class PriceWidget extends StatelessWidget {
  final String price;
  final Color? color;

  const PriceWidget({
    super.key,
    required this.price,
    this.color = const Color.fromRGBO(255, 0, 0, 1),
  });

  List<String> _priceSplit(String? value) {
    if (value == null || value.isEmpty) {
      return ['0', '00'];
    }
    var parts = value.split(".");
    if (parts.length == 1) {
      parts.add('00');
    } else if (parts[1].length == 1) {
      parts[1] = parts[1] + '0'; // 保證兩位小數
    }
    return parts;
  }

  @override
  Widget build(BuildContext context) {
    final effectiveColor = color ?? const Color.fromRGBO(255, 0, 0, 1);

    return RichText(
      text: TextSpan(
        text: "￥",
        style: TextStyle(
          fontSize: 12,
          color: effectiveColor,
        ),
        children: [
          TextSpan(
            text: _priceSplit(price)[0],
            style: TextStyle(
              fontSize: 16,
              color: effectiveColor,
            ),
          ),
          TextSpan(
            text: '.${_priceSplit(price)[1]}',
            style: TextStyle(
              fontSize: 12,
              color: effectiveColor,
            ),
          )
        ],
      ),
    );
  }
}
